#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of MPI with heterogeneous jobs.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "WARNING: ..." with an explanation of why the test can't be made, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2002 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set test_id            "38.7"
set exit_code          0
set file_in            "test$test_id.input"
set file_out           "test$test_id.output"
set file_err           "test$test_id.error"
set test_prog          "test$test_id.prog"

print_header $test_id

#
# Test for existence of mpi compiler
#
if {[info exists mpicc] == 0} {
	send_user "\nWARNING: mpicc not defined, can't perform MPI testing\n"
	exit 0
}
if {[file executable $mpicc] == 0} {
	send_user "\nWARNING: $mpicc does not exists\n"
	exit 0
}

if {[test_front_end]} {
        send_user "\nWARNING: This test is incompatible with front-end systems\n"
        exit 0
}

set def_part_name [default_partition]
set nb_nodes [get_node_cnt_in_part $def_part_name]
if {$nb_nodes < 2} {
	send_user "\nWARNING: Need 2 or more nodes in default partition\n"
	exit $exit_code
}

#
# Delete left-over program and rebuild it
#
exec $bin_rm -f $test_prog ${test_prog}.o
if {$use_pmi} {
	spawn $mpicc -Xlinker -rpath $slurm_dir/lib -L $slurm_dir/lib -lpmi -o $test_prog ${test_prog}.c
	expect {
		eof {
			wait
		}
	}
} else {
	spawn $mpicc -o $test_prog ${test_prog}.c
	expect {
		eof {
			wait
		}
	}
}
if {![file exists $test_prog]} {
	send_user "\nWARNING: Unable to build test program\n"
	exit 0
}

########################################################################
# TEST OF HET GROUP 0
########################################################################

send_user "\n\nTEST OF HET GROUP 0\n\n"

# Delete left-over stdout/err files
file delete $file_out $file_err

#
# Build input script file
#
make_bash_script $file_in "
  $bin_date
  $bin_echo HET GROUP 0
  $srun --het-group=0 ./$test_prog
  $bin_date
  $bin_echo TEST_COMPLETE
"

#
# Spawn an sbatch job that uses stdout/err and confirm their contents
#
set timeout $max_job_delay
set job_id   0
set no_start 0
set sbatch_pid [spawn $sbatch -N1 -n2 --output=$file_out --error=$file_err -t1 : -N1 -n2 ./$file_in]
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re "Batch job submission failed" {
		set no_start 1
		exp_continue
	}
	-re "Unable to contact" {
		send_user "\nFAILURE: slurm appears to be down\n"
		exit 1
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $sbatch_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$no_start != 0} {
	send_user "\nWARNING: partition too small for test\n"
	if {$job_id != 0} {
		cancel_job $job_id
	}
	exit 0
}
if {$job_id == 0} {
	send_user "\nFAILURE: batch submit failure\n"
	exit 1
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job $job_id to complete\n"
	cancel_job $job_id
	set exit_code 1
}

#
# Check for desired output in stdout
#
if {[wait_for_file $file_out] == 0} {
	set complete 0
	set expected_sum 2
	set expected_msg 2
	set matches  0
	set rank_sum 0
	spawn $bin_cat $file_out
	expect {
                -re "Rank.($number). on $alpha_numeric_under just received msg from Rank ($number)" {
			incr rank_sum $expect_out(1,string)
			incr rank_sum $expect_out(2,string)
			incr matches
                        exp_continue
                }
		-re "TEST_COMPLETE" {
			incr complete
                        exp_continue
		}
		eof {
			wait
		}
        }
	if {$matches == 0} {
		send_user "\nFAILURE: No MPI communications occurred\n"
		send_user "  The version of MPI you are using may be incompatible "
		send_user "with the configured switch\n"
		send_user "  Core files may be present from failed MPI tasks\n\n"
		set exit_code 1
	} elseif {$matches != $expected_msg} {
		send_user "\nFAILURE: unexpected output ($matches of $expected_msg)\n"
		set exit_code 1
	} elseif {$complete == 0} {
		send_user "\nFAILURE: test failed to complete\n"
		set exit_code 1
	} elseif {$rank_sum != $expected_sum} {
		send_user "\nFAILURE: Invalid rank values ($rank_sum != $expected_sum)\n"
		set exit_code 1
	}
} else {
	set exit_code 1
}

########################################################################
# TEST OF HET GROUP 1
########################################################################

send_user "\n\nTEST OF HET GROUP 1\n\n"

# Delete left-over stdout/err files
file delete $file_out $file_err

#
# Build input script file
#
make_bash_script $file_in "
  $bin_date
  $bin_echo HET GROUP 1
  $srun --het-group=1 ./$test_prog
  $bin_date
  $bin_echo TEST_COMPLETE
"

#
# Spawn an sbatch job that uses stdout/err and confirm their contents
#
set job_id   0
set no_start 0
set sbatch_pid [spawn $sbatch -N1 -n2 --output=$file_out --error=$file_err -t1 : -N1 -n2 ./$file_in]
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re "Batch job submission failed" {
		set no_start 1
		exp_continue
	}
	-re "Unable to contact" {
		send_user "\nFAILURE: slurm appears to be down\n"
		exit 1
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $sbatch_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$no_start != 0} {
	send_user "\nWARNING: partition too small for test\n"
	if {$job_id != 0} {
		cancel_job $job_id
	}
	exit 0
}
if {$job_id == 0} {
	send_user "\nFAILURE: batch submit failure\n"
	exit 1
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job $job_id to complete\n"
	cancel_job $job_id
	set exit_code 1
}

#
# Check for desired output in stdout
#
if {[wait_for_file $file_out] == 0} {
	set complete 0
	set expected_sum 2
	set expected_msg 2
	set matches  0
	set rank_sum 0
	spawn $bin_cat $file_out
	expect {
                -re "Rank.($number). on $alpha_numeric_under just received msg from Rank ($number)" {
			incr rank_sum $expect_out(1,string)
			incr rank_sum $expect_out(2,string)
			incr matches
                        exp_continue
                }
		-re "TEST_COMPLETE" {
			incr complete
                        exp_continue
		}
		eof {
			wait
		}
        }
	if {$matches == 0} {
		send_user "\nFAILURE: No MPI communications occurred\n"
		send_user "  The version of MPI you are using may be incompatible "
		send_user "with the configured switch\n"
		send_user "  Core files may be present from failed MPI tasks\n\n"
		set exit_code 1
	} elseif {$matches != $expected_msg} {
		send_user "\nFAILURE: unexpected output ($matches of $expected_msg)\n"
		set exit_code 1
	} elseif {$complete == 0} {
		send_user "\nFAILURE: test failed to complete\n"
		set exit_code 1
	} elseif {$rank_sum != $expected_sum} {
		send_user "\nFAILURE: Invalid rank values ($rank_sum != $expected_sum)\n"
		set exit_code 1
	}
} else {
	set exit_code 1
}

########################################################################
# TEST OF HET GROUP 0,1
########################################################################

if {[test_hetjob_step] == 0} {
	if {$exit_code == 0} {
		exec $bin_rm -f $file_in $file_out $file_err $test_prog ${test_prog}.o
	}
	send_user "\nWARNING: heterogeneous steps not currently supported\n"
	exit $exit_code
}

#
# OpenMPI can only run in multi-slurmd mode if no more than one node has
# more than one task. Individual nodes with more than one task use shared
# memory for communications and if more than one node is doing that, their
# shared memory use collides. That means these MPI tests will work if five
# nodes or more are available, otherwise some tests will fail. See test1.117
# for a variation of this test that will work with OpenMPI and multi-slurmd
# mode.
#
if {[test_multiple_slurmd]} {
	if {$exit_code == 0} {
		exec $bin_rm -f $file_in $file_out $file_err $test_prog ${test_prog}.o
	}
	send_user "\nWARNING: This test is incompatible with multiple slurmd systems\n"
	exit $exit_code
}

send_user "\n\nTEST OF HET GROUP 0,1\n\n"

# Delete left-over stdout/err files
file delete $file_out $file_err

#
# Build input script file
#
make_bash_script $file_in "
  $bin_date
  $bin_echo HET GROUP 0,1
  $srun --het-group=0,1 ./$test_prog
  $bin_date
  $bin_echo TEST_COMPLETE
"

#
# Spawn an sbatch job that uses stdout/err and confirm their contents
#
set job_id   0
set no_start 0
set sbatch_pid [spawn $sbatch -N1 -n2 --output=$file_out --error=$file_err -t1 : -N1 -n2 ./$file_in]
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re "Batch job submission failed" {
		set no_start 1
		exp_continue
	}
	-re "Unable to contact" {
		send_user "\nFAILURE: slurm appears to be down\n"
		exit 1
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $sbatch_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$no_start != 0} {
	send_user "\nWARNING: partition too small for test\n"
	if {$job_id != 0} {
		cancel_job $job_id
	}
	exit 0
}
if {$job_id == 0} {
	send_user "\nFAILURE: batch submit failure\n"
	exit 1
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job $job_id to complete\n"
	cancel_job $job_id
	set exit_code 1
}

#
# Check for desired output in stdout
#
if {[wait_for_file $file_out] == 0} {
	set complete 0
	set expected_sum 12
	set expected_msg 4
	set matches  0
	set rank_sum 0
	spawn $bin_cat $file_out
	expect {
                -re "Rank.($number). on $alpha_numeric_under just received msg from Rank ($number)" {
			incr rank_sum $expect_out(1,string)
			incr rank_sum $expect_out(2,string)
			incr matches
                        exp_continue
                }
		-re "TEST_COMPLETE" {
			incr complete
                        exp_continue
		}
		eof {
			wait
		}
        }
	if {$matches == 0} {
		send_user "\nFAILURE: No MPI communications occurred\n"
		send_user "  The version of MPI you are using may be incompatible "
		send_user "with the configured switch\n"
		send_user "  Core files may be present from failed MPI tasks\n\n"
		set exit_code 1
	} elseif {$matches != $expected_msg} {
		send_user "\nFAILURE: unexpected output ($matches of $expected_msg)\n"
		set exit_code 1
	} elseif {$complete == 0} {
		send_user "\nFAILURE: test failed to complete\n"
		set exit_code 1
	} elseif {$rank_sum != $expected_sum} {
		send_user "\nFAILURE: Invalid rank values ($rank_sum != $expected_sum)\n"
		set exit_code 1
	}
} else {
	set exit_code 1
}

########################################################################
# TEST OF HET GROUP 1 THEN 0,1
########################################################################

send_user "\n\nTEST OF HET GROUP 1 THEN 0,1\n\n"

# Delete left-over stdout/err files
file delete $file_out $file_err

#
# Build input script file
#
make_bash_script $file_in "
  $bin_date
  $bin_echo HET GROUP 1
  $srun --het-group=1 ./$test_prog
  $bin_echo HET GROUP 0,1
  $srun --het-group=0,1 ./$test_prog
  $bin_date
  $bin_echo TEST_COMPLETE
"

#
# Spawn an sbatch job that uses stdout/err and confirm their contents
#
set job_id   0
set no_start 0
set sbatch_pid [spawn $sbatch -N1 -n2 --output=$file_out --error=$file_err -t1 : -N1 -n2 ./$file_in]
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re "Batch job submission failed" {
		set no_start 1
		exp_continue
	}
	-re "Unable to contact" {
		send_user "\nFAILURE: slurm appears to be down\n"
		exit 1
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $sbatch_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$no_start != 0} {
	send_user "\nWARNING: partition too small for test\n"
	if {$job_id != 0} {
		cancel_job $job_id
	}
	exit 0
}
if {$job_id == 0} {
	send_user "\nFAILURE: batch submit failure\n"
	exit 1
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job $job_id to complete\n"
	cancel_job $job_id
	set exit_code 1
}

#
# Check for desired output in stdout
#
if {[wait_for_file $file_out] == 0} {
	set complete 0
	set expected_sum 14
	set expected_msg 6
	set matches  0
	set rank_sum 0
	spawn $bin_cat $file_out
	expect {
                -re "Rank.($number). on $alpha_numeric_under just received msg from Rank ($number)" {
			incr rank_sum $expect_out(1,string)
			incr rank_sum $expect_out(2,string)
			incr matches
                        exp_continue
                }
		-re "TEST_COMPLETE" {
			incr complete
                        exp_continue
		}
		eof {
			wait
		}
        }
	if {$matches == 0} {
		send_user "\nFAILURE: No MPI communications occurred\n"
		send_user "  The version of MPI you are using may be incompatible "
		send_user "with the configured switch\n"
		send_user "  Core files may be present from failed MPI tasks\n\n"
		set exit_code 1
	} elseif {$matches != $expected_msg} {
		send_user "\nFAILURE: unexpected output ($matches of $expected_msg)\n"
		set exit_code 1
	} elseif {$complete == 0} {
		send_user "\nFAILURE: test failed to complete\n"
		set exit_code 1
	} elseif {$rank_sum != $expected_sum} {
		send_user "\nFAILURE: Invalid rank values ($rank_sum != $expected_sum)\n"
		set exit_code 1
	}
} else {
	set exit_code 1
}

if {$exit_code == 0} {
	exec $bin_rm -f $file_in $file_out $file_err $test_prog ${test_prog}.o
	send_user "\nSUCCESS\n"
} else {
	set matches 0
	spawn head $file_err
	expect {
		-re "Error creating CQ" {
			incr matches
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$matches != 0} {
		send_user "WARNING: If using MVAPICH then\n"
		send_user "         Configure \"PropagateResourceLimitsExcept=MEMLOCK\"\n"
		send_user "         Also start slurmd with \"ulimit -l unlimited\"\n"
	} else {
		send_user "Check contents of $file_err\n"
	}
}

exit $exit_code
